package com.ohc.leetcode.栈.repeat;

import java.util.HashMap;
import java.util.Stack;

/**
 * 2022年5月4日20:07:19
 */
public class 有效的括号03 {
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        HashMap<Character, Character> map = new HashMap<>();
        map.put(')', '(');
        map.put(']', '[');
        map.put('}', '{');
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '(') {
                stack.push(chars[i]);
            } else if (chars[i] == '{') {
                stack.push(chars[i]);
            } else if (chars[i] == '[') {
                stack.push(chars[i]);
            } else {
                Character pop = stack.pop();
                Character s1 = map.get(chars[i]);
                if (pop != s1) {
                    return false;
                }
            }
        }

        return true;
    }


    public static void main(String[] args) {
        boolean valid = isValid("()[]{}");
        System.out.println("valid = " + valid);
    }
}
